#include "tg_documentview.h"
#include "tg_zoom.h"

#include <QTransform>
#include <QWheelEvent>

TgDocumentView::TgDocumentView(QWidget *parent) :
    BaseClass(parent),
    m_zoom(TgZoom::defaultZoom)
{
}

void TgDocumentView::zoomIn()
{
    setZoom(m_zoom + 10);
}

void TgDocumentView::zoomOut()
{
    setZoom(m_zoom - 10);
}

void TgDocumentView::setZoom(int zoom)
{
    zoom = TgZoom::validZoom(zoom);
    if(m_zoom != zoom)
    {
        m_zoom = zoom;
        qreal rZoom = (qreal)m_zoom / 100.;
        setTransform(QTransform().scale(rZoom, rZoom), false);
        emit zoomChanged();
    }
}

void TgDocumentView::wheelEvent(QWheelEvent * event)
{
    if(event->modifiers() & Qt::ControlModifier)
    {
        if(event->delta() > 0)
        {
            zoomIn();
        }
        else
        {
            zoomOut();
        }
        event->accept();
    }
}
